#include "light.h"
#include <hosal_pwm.h>
#include <stdio.h>
#include "file_op.h"
#include "configs.h"

static hosal_pwm_dev_t light;

void set_light_brightness(uint8_t duty)
{
    hosal_pwm_config_t para;
    para.duty_cycle = duty * 100;
    para.freq = 3840;
    hosal_pwm_para_chg(&light, para);

    write_file(LIGHT_FILE, (const char *)&duty, 1);
}

uint8_t get_light_brightness(void)
{
    uint8_t duty;
    read_file(LIGHT_FILE, (char *)&duty, 1);
    
    return duty;
}


void light_out(uint8_t value)
{
    printf("light_out:%d\n", value);
    if (value) {
        hosal_pwm_start(&light);
    } else {
        hosal_pwm_stop(&light);
    }
}

void init_light(void)
{
    int ret;
    uint8_t duty;

    ret = read_file(LIGHT_FILE, (char *)&duty, 1);
    if (ret != 1) {
        duty = 100;
    }

    light.config.pin = 3;
    light.config.duty_cycle = duty * 100; //占空比范围为：0~10000相当于0~100%
    light.config.freq = 3840;             //频率范围为 0~40MHZ
    light.port = light.config.pin % 5;    // 端口 = 引脚 % 5
    hosal_pwm_init(&light);
}
